<template>
  <h1 class="manageTitle">广东海洋大学人员管理系统</h1>
  <div class="login-wrapper">
    <div class="back"><img src="./../assets/images/Welcome.jpeg" alt=""></div>
    <div class="modal">
      <el-form ref="userFrom" :model="userInfo" status-icon :rules="rules">
        <div class="title">修改密码</div>
        <el-form-item prop="userOldPwd">
          <el-input type="password" prefix-icon="el-icon-view" v-model="userInfo.userOldPwd"></el-input>
        </el-form-item>

        <el-form-item prop="userNewPwd">
          <el-input type="password" prefix-icon="el-icon-view" v-model="userInfo.userNewPwd"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button class="btn-login" type="primary" @click="updatePassword">确认修改</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "updatePassword",
  data(){
    return{
      user:this.$store.state.userInfo,
      //登陆用户信息
      userInfo:{
        userOldPwd: '',
        userNewPwd: '',
      },
      // 校验
      rules:{
        userOldPwd: [
          {
            required:true,message:"请输入旧的密码",trigger:"blur"
          }
        ],
        userNewPwd: [
          {
            required:true,message:"请输入新的密码",trigger:"blur"
          }
        ]
      }
    }
  },
  methods:{
    updatePassword(){
      let _id=this.user._id;
      let newPassword=this.userInfo.userNewPwd;
      let params={_id,newPassword}
      params.action="get"
      this.$api.updatePassword(params).then((res)=>{
        let userPwd=res[0].userPwd;
        if(this.userInfo.userOldPwd==""){
          this.$message.success("请输入旧的密码");
          return;
        }
        if(this.userInfo.userNewPwd==""){
          this.$message.success("请输入新的密码");
          return;
        }
        if(this.userInfo.userOldPwd!==userPwd){
          this.$message.success("输入密码不正确，请重新输入");
          this.userInfo.userNewPwd="";
          this.userInfo.userOldPwd=""
        }else {
          if(this.userInfo.userNewPwd==this.userInfo.userOldPwd){
            alert("输入密码不得与旧的密码相同，请重新输入")
            this.userInfo.userNewPwd="";
            this.userInfo.userOldPwd=""
          }else {
            params.action="update"
            this.$api.updatePassword(params)
            this.$message.success("密码修改成功，请重新登陆");
            this.userInfo = {};
            this.$router.push('/login')
          }
        }

      })

    }
  }
}

</script>

<style lang="scss">
.manageTitle{
  width: 100%;
  height: 100px;
  line-height: 150px;
  text-align: center;
  letter-spacing: 5px;
  font-size: 40px;
  color: #409eff;
  font-weight: 700;
  background-color: #f9fcff;
}
.login-wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f9fcff;
  width: 100vw;
  height: 85vh;
  .back{
    width: 500px;
    height: 327px;
    overflow: hidden;
    margin-right: 100px;
    border-radius: 20px;
    box-shadow: 0 0  10px 3px #c7c9cb4d;
    img{
      width: 500px;
      height: 327px;
      overflow: clip;
    }
  }
  .modal {
    width: 500px;
    padding: 50px;
    background-color: #fff;
    border-radius: 4px;
    box-shadow: 0 0  10px 3px #c7c9cb4d;
    .title {
      font-size: 30px;
      line-height: 1.5;
      text-align: center;
      margin-bottom: 30px;
    }
    .btn-login {
      width: 100%;
    }
  }
}
</style>
